import 'package:aichat_flutter_wallet/module_common/utils/colors.dart';
import 'package:aichat_flutter_wallet/module_common/widgets/nodeSpeed/nodeSpeed.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:lib_database/lib_database.dart';
import 'package:lib_util/lib_util.dart';

import '../../../module_common/utils/util.dart';
import '../../../module_common/widgets/loading/small_loading.dart';
import '../../module_trade/widgets/triangle_painter.dart';

class NodeRow extends StatefulWidget {
  NodeInfo node;
  bool isSelected;
  bool isLoading;
  ValueNotifier<int> notifi;

  NodeRow(
      {Key? key,
      this.isLoading=false,
      required this.notifi,
      required this.node,
      required this.isSelected})
      : super(key: key);

  @override
  _NodeRowState createState() => _NodeRowState();
}

class _NodeRowState extends State<NodeRow> {
  bool get isLoading => widget.isLoading;

  @override
  void initState() {
    super.initState();
    //初始化
  }

  Widget nodeSpeed(_currentRate, _currentColor,_isLoading) {
    return Row(
      children: [
        Text(
          _currentRate.toString() + 'ms',
          style: TextStyle(
              color: _currentColor,
              fontSize: 10.sp,
              fontWeight: FontWeight.w400),
        ),
        SizedBox(
          width: 2,
        ),
        NodeSpeed(
          color: _currentColor,
          size: 10,
        ),
        Visibility(
          visible:_isLoading,
          child: SmallLoading(
          width: 12.w,
          height: 12.w,
        ),)
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    String rate =  widget.node.blockHeight == '0'? '0': (widget.node.rate??'0');
    return Padding(
      padding: EdgeInsets.only(left: 20.w),
      child: Stack(
        children: [
          Container(
            height: 60.w,
            decoration: BoxDecoration(
                color: widget.isSelected
                    ? Color.fromRGBO(134, 103, 254, 0.05)
                    : Color.fromRGBO(246, 246, 246, 1),
                border: widget.isSelected
                    ? Border.all(
                        color: Color.fromRGBO(134, 103, 254, 1), width: 1.w)
                    : Border.all(color: Colors.transparent, width: 1.w),
                borderRadius: BorderRadius.all(Radius.circular(10.w))),
            child: SizedBox(
              child: Row(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  SizedBox(width: 15.w),
                  Expanded(
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Row(
                          crossAxisAlignment: CrossAxisAlignment.center,
                          children: [
                            Text(widget.node.name ?? 'Official',
                                style: TextStyle(
                                    fontSize: 14.sp,
                                    fontWeight: FontWeight.w500,
                                    color: "#333333".toColor())),
                            SizedBox(
                              width: 10.w,
                            ),
                            nodeSpeed(rate, NodeRateUtils.getCurrentRateColor(currentRate: rate),isLoading)
                          ],
                        )
                        //ObjectBox().nodeListModel.initNode();
                        ,
                        Text(widget.node.url,
                            maxLines: 1,
                            overflow: TextOverflow.ellipsis,
                            style: TextStyle(
                                fontSize: 10.sp,
                                fontWeight: FontWeight.w400,
                                color: "#868391".toColor())),
                      ],
                    ),
                  ),
                  Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    crossAxisAlignment: CrossAxisAlignment.end,
                    children: [
                      Text(
                        widget.node.blockHeight!,
                        style: TextStyle(
                            fontSize: 12.sp,
                            fontWeight: FontWeight.w400,
                            color: AppColors.c_333333),
                      ),
                      SizedBox(
                        height: 5.w,
                      ),
                      Text(
                        'Block height',
                        style: TextStyle(
                            fontSize: 10.sp,
                            fontWeight: FontWeight.w400,
                            color: AppColors.c_868391),
                      ),
                    ],
                  ),
                  SizedBox(width: 15.w),
                ],
              ),
            ),
          ),
          if (widget.isSelected)
            Positioned(
              bottom: 1,
              right: 1,
              child: CustomPaint(
                painter: TrianglePainter(),
                child: Container(
                  width: 20.w,
                  height: 20.w,
                  alignment: Alignment.bottomRight,
                  child: Image.asset(
                    'assets/images/wallet-trade/selected.png',
                    width: 14.w,
                    height: 14.w,
                  ),
                ),
              ),
            )
        ],
      ),
    );
  }

  Widget titleWidget(String title) {
    return Text(title,
        style: TextStyle(
            fontSize: 16.sp,
            fontWeight: FontWeight.bold,
            color: "#333333".toColor()));
  }

  Widget speedPoint(Color color) {
    return ClipRRect(
      borderRadius: BorderRadius.circular(6.w),
      child: Container(
        width: 6.w,
        height: 6.w,
        color: color,
      ),
    );
  }



}
